package com.wyp.didishige.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wyp.didishige.activity.UserInfoActivity;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.model.Rating;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.DateUtil;
import com.wyp.didishige.utils.ImageLoadOptions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 附近boy的人适配器
 * Created by horry on 2015/4/14.
 */
public class NearBoyAdapter extends BaseListAdapter<User> {

    public NearBoyAdapter(Context context, List<User> list) {
        super(context, list);

        this.mContext = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
    }

    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.near_boy, null);
            holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
            holder.distance = (TextView) convertView.findViewById(R.id.distance);
            holder.last_login_time = (TextView) convertView.findViewById(R.id.lastLogin_time);

            holder.serve_times = (TextView) convertView.findViewById(R.id.serve_times);
            holder.serve_declaration = (TextView) convertView.findViewById(R.id.serve_declaration);

            holder.user_head = (ImageView) convertView.findViewById(R.id.user_head);
            holder.user_gender = (ImageView) convertView.findViewById(R.id.user_gender);
            holder.rating_lay = (LinearLayout) convertView.findViewById(R.id.rating_lay);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final User user = list.get(position);

        if (user.getAvatar() == null) {
            holder.user_head.setImageResource(R.drawable.ic_launcher);
        } else {
            ImageLoader.getInstance().displayImage(user.getAvatar(), holder.user_head, ImageLoadOptions.getOptions());
        }

        holder.user_name.setText(user.getNick());
        if (user.getLevel() != 0) {
            if (user.getSex()) {
                holder.user_name.setTextColor(mContext.getResources().getColor(R.color.vip_male));
            } else {
                holder.user_name.setTextColor(mContext.getResources().getColor(R.color.vip_female));
            }
        }
        if (user.getSex() != null && user.getSex()) {
            holder.user_gender.setImageResource(R.drawable.male);
        } else {
            holder.user_gender.setImageResource(R.drawable.female);
        }

        holder.user_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(Config.KEY_USER_ID, user.getObjectId());

                bundle.putSerializable("from", "add");
                IntentUtil.start_activity((Activity) mContext, UserInfoActivity.class, bundle);
            }
        });
        if (user.getServe_declaration() != null) {
            holder.serve_declaration.setText(user.getServe_declaration());
        } else {
//            holder.serve_declaration.setText("");
        }

        double distance = user.getMeter() / 1000;

        holder.distance.setText(distance + "千米");
        Date date;
        if (user.getLastLogin() == 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = simpleDateFormat.parse(user.getUpdatedAt());
            } catch (ParseException e) {
                e.printStackTrace();
                date = new Date(System.currentTimeMillis());
            }
        } else {
            date = new Date(user.getLastLogin());
        }


        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        holder.last_login_time.setText(DateUtil.getDateSpan(calendar.getTimeInMillis() / 1000,
                DateUtil.getTimestamp(new Date())));
        if (!holder.isRating) {
            if (user.getStarts() == 0) {
                ImageView start = new ImageView(mContext);
                start.setImageResource(R.drawable.star);
                holder.rating_lay.addView(start);
            } else {
                for (int i = 0; i < user.getStarts(); i++) {
                    ImageView start = new ImageView(mContext);
                    start.setImageResource(R.drawable.star);
                    holder.rating_lay.addView(start);
                }
            }
        }

        holder.isRating = true;
        Rating rating = user.getRating();
        if (rating != null) {
            holder.serve_times.setText(rating.getServiceNum() + "");
        } else {
            holder.serve_times.setText("0");
        }

        return convertView;
    }

    class ViewHolder {
        TextView user_name;
        TextView last_login_time;
        TextView distance;
        TextView serve_times;
        TextView serve_declaration;

        ImageView user_head;
        ImageView user_gender;
        LinearLayout rating_lay;
        boolean isRating = false;//是否添加start的标识
    }
}
